package com.ruoyi.module.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.page.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.module.app.domain.Tenant;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.module.app.service.ITenantService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 租户Controller
 *
 * @author mars
 * @date 2022-07-31
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/app/tenant" )
public class TenantController extends BaseController {

    private final ITenantService iTenantService;

    /**
     * 查询租户列表
     */
    @PreAuthorize("@ss.hasPermi('app:tenant:list')")
    @GetMapping("/list")
    public PageInfo list(Tenant tenant) {
        return iTenantService.findPageBySpec(tenant);
    }

    /**
     * 获取租户详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:tenant:query')" )
    @GetMapping(value = "/query/{id}" )
    public Result getInfo(@PathVariable("id" ) Integer id) {
        return Result.success(iTenantService.getById(id));
    }

    /**
     * 新增租户
     */
    @PreAuthorize("@ss.hasPermi('app:tenant:add')" )
    @Log(title = "租户" , businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody Tenant tenant) {
        return Result.success("新增成功",iTenantService.add(tenant));
    }

    /**
     * 修改租户
     */
    @PreAuthorize("@ss.hasPermi('app:tenant:edit')" )
    @Log(title = "租户" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody Tenant tenant) {
        return toAjax(iTenantService.update(tenant) ? 1 : 0);
    }

    /**
     * 删除租户
     */
    @PreAuthorize("@ss.hasPermi('app:tenant:remove')" )
    @Log(title = "租户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}" )
    public Result remove(@PathVariable Integer[] ids) {
        return toAjax(iTenantService.deleteBatch(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 导出租户列表
     */
    @PreAuthorize("@ss.hasPermi('app:tenant:export')" )
    @Log(title = "租户" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public Result export(Tenant tenant) {
        LambdaQueryWrapper<Tenant> lqw = new LambdaQueryWrapper<Tenant>(tenant);
        List<Tenant> list = iTenantService.list(lqw);
        ExcelUtil<Tenant> util = new ExcelUtil<Tenant>(Tenant. class);
        return util.exportExcel(list, "tenant" );
    }
}
